<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div class="header">
        <div>
          <el-select v-model="searchTerms['typeId']" placeholder="类型" class="select" clearable filterable @change="getData">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
          <el-input v-model="searchTerms.search" placeholder="请输入名称" class="search-input" @change="getData" />
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
        <div>
          <el-button type="primary" icon="el-icon-plus" @click="toCreate">添加会员</el-button>
          <el-button type="primary" icon="el-icon-download"><a :href="modelUrl" target="_blank">下载导入模板</a></el-button>
          <el-upload
            ref="upload"
            :limit="1"
            :show-file-list="false"
            :http-request="uploadFiles"
            :before-upload="beforeUpload"
            class="upload"
            accept=".xls,.xlsx"
            action="">
            <el-button :loading="uploadLoading" type="primary" icon="el-icon-printer">快速导入</el-button>
          </el-upload>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" width="100" header-align="center" align="center" sortable />
        <el-table-column prop="name" label="名称" min-width="160" header-align="center" align="center" />
        <el-table-column prop="mobile" label="手机" min-width="120" header-align="center" align="center" />
        <el-table-column prop="email" label="邮箱" min-width="180" header-align="center" align="center" />
        <el-table-column prop="position" label="职务" width="120" header-align="center" align="center" />
        <el-table-column prop="enterprise_name" label="公司" width="120" header-align="center" align="center" />
        <el-table-column prop="education" label="学历" width="120" header-align="center" align="center" />
        <el-table-column prop="industry_type_name" label="类型" width="160" header-align="center" align="center"/>
        <el-table-column label="注册时间" min-width="200" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row['created_at'] && parseTime(scope.row['created_at'], '{y}-{m}-{d} {h}:{i}') }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-search" circle @click="toInfo(scope.row)"/>
            <el-button
              type="success"
              class="bg-green"
              size="small"
              icon="el-icon-edit"
              circle
              @click="toModify(scope.row)"/>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              circle
              @click="toDelete(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, parseTime } from '@/utils/table'
import { fileToBase64 } from '../../../utils/index'

export default {
  data() {
    return {
      downloadLoading: false,
      uploadLoading: false,
      searchTerms: {
        activity_type: '',
        state: '',
        date: [],
        search: ''
      },

      modelUrl: this.$store.getters.serverUrl + '/admin/vip/toExcel?token=' + this.$store.getters.token,

      typeList: [], // 活动类型

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 380,
      tableLoading: false,
      multipleSelection: [],
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  created() {
    this.getData()
    this.getTypeList()
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, parseTime, fileToBase64,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'vip/list'
      const data = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        search: this.searchTerms.search,
        status: this.searchTerms.status,
        typeId: this.searchTerms.typeId
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取活动类型列表
     */
    getTypeList() {
      const url = 'vip/typeList'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.typeList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    beforeUpload(file) {
      // const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      // if (!isExcel) {
      //   this.$message.error('导入失败,请检查导入的文件是否是.xlsx或.xls文件')
      // }
      // return isExcel
    },
    uploadFiles(item) {
      this.uploadLoading = true
      const url = 'vip/excleInsert'
      this.fileToBase64(item.file).then(res=>{
        const data = {
          file: res
        }
        this.$store.dispatch('UploadFile', { url, data }).then((res) => {
          this.$message.success(res.msg)
          this.uploadLoading = false
          this.getData()
          this.$refs.upload.clearFiles()
        }).catch((res) => {
          this.uploadLoading = false
          this.$message.error(res.msg)
        })
      });
    },

    toInfo(row) {
      this.$router.push({
        path: '/member/details',
        query: { id: row.id }
      })
    },

    toModify(row) {
      this.$router.push({
        path: '/member/modify',
        query: { id: row.id }
      })
    },

    toCreate() {
      this.$router.push({
        path: '/member/create'
      })
    },

    toDelete(row) {
      this.$confirm('此操作将删除该会员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'vip/delMemberVip/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch(e => {
          this.$message.error(e.msg + ',请刷新或联系管理员')
        })
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .model {
    .el-card {
      color: #fff;
      background-color: $panGreen;
      span {
        font-size: 14px;
      }
      div {
        font-size: 24px;
        margin-top: 10px;
      }
    }
  }
  .header {
    @include flex-row(center, space-between);
    flex-wrap: wrap;
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      .select {
        padding-right: 15px;
        width: 150px;
      }
      .picker {
        margin-right: 15px;
        width: 300px;
      }
      .do-btn {
        @extend .bg-green;
        margin-left: 6px;
      }
      .search-input {
        width: 180px;
      }
      .upload {
        padding: 0 10px;
      }
    }
  }
}
</style>

